﻿// -----------------------------------------------------------------------
// <copyright file="SaveGame.cs" company="Diddly Squad">
// </copyright>
// -----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;

namespace DiddlySquatDataTypes
{
	/// <summary>
	/// Class for saving and loading game play data.
	/// </summary>
	public class SaveGame
	{
		public Vector2 HeroPosition { get; set; }

		public List<EnemyData> Enemies;

		public List<CollectableData> Collectables;

		public int Level { get; set; }

		public double Health { get; set; }

		public int Points { get; set; }

		public float Timer { get; set; }

		/// <summary>
		/// Initializes a new instance of the <see cref="SaveGame"/> class.
		/// </summary>
		public SaveGame()
		{
			HeroPosition = new Vector2();
			Enemies = new List<EnemyData>();
			Collectables = new List<CollectableData>();
			Level = 0;
			Health = 100;
			Points = 0;
			Timer = 0;
		}

		/// <summary>
		/// Adds the enemy data.
		/// </summary>
		/// <param name="x">The x.</param>
		/// <param name="y">The y.</param>
		/// <param name="enemyType">Type of the enemy.</param>
		/// <param name="health">The health.</param>
		public void AddEnemyData(float x, float y, int enemyType, double health)
		{
			EnemyData data = new EnemyData(x, y, enemyType, health);
			this.Enemies.Add(data);
		}

		/// <summary>
		/// Adds the collectable data.
		/// </summary>
		/// <param name="x">The x.</param>
		/// <param name="y">The y.</param>
		public void AddCollectableData(float x, float y)
		{
			CollectableData data = new CollectableData(x, y);
			this.Collectables.Add(data);
		}

		/// <summary>
		/// Saves the game with specified filename.
		/// </summary>
		/// <param name="filename">The filename.</param>
		public void Save(string filename)
		{
			IAsyncResult res = StorageDevice.BeginShowSelector(null, null);
			if (res.IsCompleted)
			{
				StorageDevice device = StorageDevice.EndShowSelector(res);

				// Open a storage container.
				IAsyncResult result = device.BeginOpenContainer("DiddlySquat", null, null);

				// Wait for the WaitHandle to become signaled.
				result.AsyncWaitHandle.WaitOne();

				StorageContainer container = device.EndOpenContainer(result);

				// Close the wait handle.
				result.AsyncWaitHandle.Close();

				// Check to see whether the save exists.
				if (container.FileExists(filename)) // Delete it so that we can create one fresh.
					container.DeleteFile(filename);

				// Create the file.
				Stream stream = container.CreateFile(filename);

				// Convert the object to XML data and put it in the stream.
				XmlSerializer serializer = new XmlSerializer(typeof(SaveGame));

				serializer.Serialize(stream, this);

				// Close the file.
				stream.Close();
				// Dispose the container, to commit changes.
				container.Dispose();
			}
		}

		/// <summary>
		/// Deletes the savegame file with specified filename.
		/// </summary>
		/// <param name="filename">The filename.</param>
		public void Delete(string filename)
		{
			IAsyncResult res = StorageDevice.BeginShowSelector(null, null);
			if (res.IsCompleted)
			{
				StorageDevice device = StorageDevice.EndShowSelector(res);

				// Open a storage container.
				IAsyncResult result = device.BeginOpenContainer("DiddlySquat", null, null);

				// Wait for the WaitHandle to become signaled.
				result.AsyncWaitHandle.WaitOne();

				StorageContainer container = device.EndOpenContainer(result);

				// Close the wait handle.
				result.AsyncWaitHandle.Close();

				// Check to see whether the save exists.
				if (container.FileExists(filename)) // Delete it so that we can create one fresh.
					container.DeleteFile(filename);
				
				// Dispose the container, to commit changes.
				container.Dispose();
			}
		}

		/// <summary>
		/// Loads the game from specified filename.
		/// </summary>
		/// <param name="filename">The filename.</param>
		/// <returns></returns>
		public bool Load(string filename)
		{

			IAsyncResult res = StorageDevice.BeginShowSelector(null, null);
			if (res.IsCompleted)
			{

				StorageDevice device = StorageDevice.EndShowSelector(res);
				// Open a storage container.
				IAsyncResult result = device.BeginOpenContainer("DiddlySquat", null, null);

				// Wait for the WaitHandle to become signaled.
				result.AsyncWaitHandle.WaitOne();

				StorageContainer container = device.EndOpenContainer(result);

				// Close the wait handle.
				result.AsyncWaitHandle.Close();

				// Check to see whether the save exists.
				if (!container.FileExists(filename))
				{
					// If not, dispose of the container and return.
					container.Dispose();
					return false;
				}
				// Open the file.
				Stream stream = container.OpenFile(filename, FileMode.Open);
				XmlSerializer serializer = new XmlSerializer(typeof(SaveGame));
				SaveGame data = (SaveGame)serializer.Deserialize(stream);

				this.HeroPosition = data.HeroPosition;
				this.Enemies = data.Enemies;
				this.Collectables = data.Collectables;
				this.Level = data.Level;
				this.Health = data.Health;
				this.Points = data.Points;
				this.Timer = data.Timer;

				// Close the file.
				stream.Close();
				// Dispose the container.
				container.Dispose();

				return true;
			}
			return false;
		}

		/// <summary>
		/// Gets the save game.
		/// </summary>
		/// <returns></returns>
		public SaveGame GetSaveGame()
		{
			return this;
		}
	}
	
}

